package com.gopay.common.user.dao.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.CustInfoWhitelistConstants;
import com.gopay.common.domain.cust.CustInfoWhitelist;
import com.gopay.common.user.dao.CustInfoWhitelistDAO;
import com.gopay.common.util.StringTools;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 网联改造白名单
 * @author dong-wang5@hnair.com
 * @date 2018-04-09 17:10:24
 */
@Repository("custInfoWhitelistDAO")
public class CustInfoWhitelistDAOImpl extends BaseDAOImpl<CustInfoWhitelist, Serializable> implements CustInfoWhitelistDAO {
    @Override
    public CustInfoWhitelist findByCustId(String custId, String type) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustInfoWhitelist.class);
        criteria.add(Restrictions.eq("custId",custId).ignoreCase());
        criteria.add(Restrictions.eq("type", type));
        criteria.add(Restrictions.eq("status", CustInfoWhitelistConstants.CUST_INFO_STATUS_1));
        List<CustInfoWhitelist> list = this.findByCriteria(criteria);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<CustInfoWhitelist> findByCorpName(String corpName, String type) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustInfoWhitelist.class);
        criteria.add(Restrictions.eq("corpName",corpName));
        criteria.add(Restrictions.eq("type",type));
        criteria.add(Restrictions.eq("status", CustInfoWhitelistConstants.CUST_INFO_STATUS_1));
        return findByCriteria(criteria);
    }

    @Override
    public List<CustInfoWhitelist> findAllListInfoByPage(Map<String, Object> param, Integer pageIndex, Integer pageSize) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustInfoWhitelist.class);
        if(StringUtils.isNotBlank(StringTools.checkNulltoString(param.get("custId")))){
            criteria.add(Restrictions.eq("custId",StringTools.checkNulltoString(param.get("custId"))));
        }
        if(StringUtils.isNotBlank(StringTools.checkNulltoString(param.get("corpName")))){
            criteria.add(Restrictions.like("corpName","%"+StringTools.checkNulltoString(param.get("corpName")+"%")));
        }
        //按照修改时间倒序
        criteria.addOrder(Order.desc("lastUpdTs"));
        return findByCriteria(criteria,pageIndex,pageSize);
    }

    @Override
    public Integer findAllListInfoCountByCondition(Map<String, Object> param) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustInfoWhitelist.class);
        if(StringUtils.isNotBlank(StringTools.checkNulltoString(param.get("custId")))){
            criteria.add(Restrictions.eq("custId",StringTools.checkNulltoString(param.get("custId"))));
        }
        if(StringUtils.isNotBlank(StringTools.checkNulltoString(param.get("corpName")))){
            criteria.add(Restrictions.like("corpName","%"+StringTools.checkNulltoString(param.get("corpName")+"%")));
        }
        List<CustInfoWhitelist>  whiteLists = super.findByCriteria(criteria);
        if(whiteLists == null){
            return 0;
        }else{
            return whiteLists.size();
        }
    }

    @Override
    public CustInfoWhitelist findListInfoById(Long id) {
        CustInfoWhitelist whiteList = super.find(CustInfoWhitelist.class,id);
        return whiteList;
    }
}
